import request from '@/utils/request.js';
import { REGEO_KEY, REGEO_PRIVATE_KEY } from '@/config/app.js';
//引入md5
import md5 from 'js-md5';

// 地址列表
export function getAddressList() {
    return request.get('/address/list');
}

// 添加新地址
export function AddressList(data) {
    return request.post('/address/add', data);
}

// 获取省份
export function getprovince() {
    return request.get('/getprovince');
}

// 获取市区县
export function getcity(data) {
    return request.get('/getcity', data);
}

// 修改地址
export function updateAddress(data) {
    return request.post('/address/update', data);
}

// 删除地址
export function deleteAddress(data) {
    return request.post('/address/delete', data);
}

// 提交意见反馈
export function submitFeedback(data) {
    return request.post('/Feedback/submit', data);
}
// 提交意见反馈list
export function feedbackList(data) {
    return request.post('/feedbacklist', data);
}
// 提交意见反馈详情
export function feedbackDetail(data) {
    return request.post('/feedbackdetail', data);
}

// 设置默认地址
export function setdefaultAddress(data) {
    return request.post('/address/setdefault', data);
}

// 获取详细地址
export function getDetailAddress(data) {
    return request.get(`/address/detail/${data}`);
}

// 获取默认地址
export function geDefaultAddress() {
    return request.get(`/address/default`);
}


// 我的投诉list
export function complaintList(data) {
    return request.post('/complaintlist', data);
}

// 高德地图逆地理编码


export function getRegeo(data) {
    // 私钥
    const privateKey = REGEO_PRIVATE_KEY;

    // 构建参数对象
    const params = {
        extensions: 'all',
        key: REGEO_KEY,
        location: data.location,
        poitype: data.poitype,
        radius: '1000',
        roadlevel: '0'
    };

    // 按参数名升序排序
    const sortedKeys = Object.keys(params).sort();
    // 拼接成 a=1&b=2... 格式
    const paramStr = sortedKeys.map(k => `${k}=${params[k]}`).join('&');
    // 拼接私钥
    const signStr = paramStr + privateKey;
    // 计算md5
    const sig = md5(signStr);


    // 最终url
    const url = `https://restapi.amap.com/v3/geocode/regeo?${paramStr}&sig=${sig}`;

    return new Promise((resolve, reject) => {
        uni.request({
            url: url,
            method: 'GET',
            success: (res) => {
                resolve(res)
            },
            fail: (err) => {
                reject(err)
            }
        })
    })
}